home *** CD-ROM | disk | FTP | other *** search
/ Aminet 35 / Aminet 35 (2000)(Schatztruhe)[!][Feb 2000].iso / Aminet / gfx / misc / gnuplot-src.lha / gnuplot-3.7.1src / gnuplot-3.7.1.lha / gnuplot-3.7.1 / docs / doc2ms.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-19  |  7.6 KB  |  328 lines

  1. #ifndef lint0î¬î2á„Sid = "$Id: doc2ms.c,v 1.9 1998/10/19 13:17:49 lhecking Exp $";
  2. #endif
  3.  
  4. /* GNUPLOT - doc2ms.c */
  5.  
  6. /*[
  7.  * Copyright 1986 - 1993, 1998   Thomas Williams, Colin Kelley
  8.  *
  9.  * Permission to use, copy, and distribute this software and its
  10.  * documentation for any purpose with or without fee is hereby granted,
  11.  * provided that the above copyright notice appear in all copies and
  12.  * that both that copyright notice and this permission notice appear
  13.  * in supporting documentation.
  14.  *
  15.  * Permission to modify the software is granted, but not the right to
  16.  * distribute the complete modified source code.  Modifications are to
  17.  * be distributed as patches to the released version.  Permission to
  18.  * distribute binaries produced by compiling modified sources is granted,
  19.  * provided you
  20.  *   1. distribute the corresponding source modifications from the
  21.  *    released version in the form of a patch file along with the binaries,
  22.  *   2. add special version identification to distinguish your version
  23.  *    in addition to the base release version number,
  24.  *   3. provide your name and address as the primary contact for the
  25.  *    support of your modified version, and
  26.  *   4. retain our contact information in regard to use of the base
  27.  *    software.
  28.  * Permission to distribute the released version of the source code along
  29.  * with corresponding source modifications in the form of a patch file is
  30.  * granted with same provisions 2 through 4 for binary distributions.
  31.  *
  32.  * This software is provided "as is" without express or implied warranty
  33.  * to the extent permitted by applicable law.
  34. ]*/
  35.  
  36. /*
  37.  * doc2ms.c  -- program to convert Gnuplot .DOC format to *roff -ms document
  38.  * From hlp2ms by Thomas Williams 
  39.  *
  40.  * Modified by Russell Lang, 2nd October 1989
  41.  * to make vms help level 1 and 2 create the same ms section level.
  42.  *
  43.  * Modified to become doc2ms by David Kotz (David.Kotz@Dartmouth.edu) 12/89
  44.  * Added table and backquote support.
  45.  *
  46.  * usage:  doc2ms [file.doc [file.ms]]
  47.  *
  48.  *   where file.doc is a VMS .DOC file, and file.ms will be a [nt]roff
  49.  *     document suitable for printing with nroff -ms or troff -ms
  50.  *
  51.  * typical usage for GNUPLOT:
  52.  *
  53.  *   doc2ms gnuplot.doc | tbl | eqn | troff -ms
  54.  *
  55.  * or
  56.  *
  57.  *   doc2ms gnuplot.doc | groff -ms -et >gnuplot.ps
  58.  */
  59.  
  60. #ifdef HAVE_CONFIG_H
  61. # include "config.h"
  62. #endif
  63.  
  64. #include "ansichek.h"
  65. #include "stdfn.h"
  66. #include "doc2x.h"
  67.  
  68. #define LINE_SKIP        3
  69.  
  70. void init __PROTO((FILE *));
  71. void convert __PROTO((FILE *, FILE *));
  72. void process_line __PROTO((char *, FILE *));
  73. void section __PROTO((char *, FILE *));
  74. void putms __PROTO((char *, FILE *));
  75. void putms_verb __PROTO((char *, FILE *));
  76. void finish __PROTO((FILE *));
  77.  
  78. static boolean intable = FALSE;
  79.  
  80. int main(argc, argv)
  81. int argc;
  82. char **argv;
  83. {
  84.     FILE *infile;
  85.     FILE *outfile;
  86.     infile = stdin;
  87.     outfile = stdout;
  88.     if (argc > 3) {
  89.     fprintf(stderr, "Usage: %s [infile [outfile]]\n", argv[0]);
  90.     exit(EXIT_FAILURE);
  91.     }
  92.     if (argc >= 2) {
  93.     if ((infile = fopen(argv[1], "r")) == (FILE *) NULL) {
  94.         fprintf(stderr, "%s: Can't open %s for reading\n",
  95.             argv[0], argv[1]);
  96.         exit(EXIT_FAILURE);
  97.     }
  98.     }
  99.     if (argc == 3) {
  100.     if ((outfile = fopen(argv[2], "w")) == (FILE *) NULL) {
  101.         fprintf(stderr, "%s: Can't open %s for writing\n",
  102.             argv[0], argv[2]);
  103.         exit(EXIT_FAILURE);
  104.     }
  105.     }
  106.     init(outfile);
  107.     convert(infile, outfile);
  108.     finish(outfile);
  109.     exit(EXIT_SUCCESS);
  110. }
  111.  
  112.  
  113. void init(b)
  114. FILE *b;
  115. {
  116.     /* in nroff, increase line length by 8 and don't adjust lines */
  117.     (void) fputs(".if n \\{.nr LL +8m\n.na \\}\n\
  118. .nr PO +0.3i\n\
  119. .so titlepag.ms\n\
  120. .pn 1\n\
  121. .bp\n\
  122. .ta 1.5i 3.0i 4.5i 6.0i 7.5i\n\
  123. \\&\n.sp 3\n.PP\n", b);
  124.  
  125.     /* following line commented out by rjl
  126.        (void) fputs(".so intro\n",b);
  127.      */
  128. }
  129.  
  130.  
  131. void convert(a, b)
  132. FILE *a, *b;
  133. {
  134.     static char line[MAX_LINE_LEN+1];
  135.  
  136.     while (get_line(line, sizeof(line), a)) {
  137.     process_line(line, b);
  138.     }
  139. }
  140.  
  141. void process_line(line, b)
  142. char *line;
  143. FILE *b;
  144. {
  145.     switch (line[0]) {        /* control character */
  146.     case '?':{            /* interactive help entry */
  147.         break;        /* ignore */
  148.     }
  149.     case '@':{            /* start/end table */
  150.         if (intable) {
  151.         (void) fputs(".TE\n.KE\n", b);
  152.         (void) fputs(".EQ\ndelim off\n.EN\n\n", b);
  153.         intable = FALSE;
  154.         } else {
  155.         (void) fputs("\n.EQ\ndelim $$\n.EN\n", b);
  156.         (void) fputs(".KS\n.TS\ncenter box tab (@) ;\n", b);
  157.         /* moved to gnuplot.doc by RCC
  158.            (void) fputs("c c l .\n", b);
  159.          */
  160.         intable = TRUE;
  161.         }
  162.         /* ignore rest of line */
  163.         break;
  164.     }
  165.     case '^':{            /* html table entry */
  166.         break;        /* ignore */
  167.     }
  168.     case '#':{            /* latex table entry */
  169.         break;        /* ignore */
  170.     }
  171.     case '%':{            /* troff table entry */
  172.         if (intable)
  173.         (void) fputs(line + 1, b);    /* copy directly */
  174.         else
  175.         fprintf(stderr, "error: %% line found outside of table\n");
  176.         break;
  177.     }
  178.     case '\n':            /* empty text line */
  179.     case ' ':{            /* normal text line */
  180.         if (intable)
  181.         break;        /* ignore while in table */
  182.         switch (line[1]) {
  183.         case ' ':{
  184.             /* verbatim mode */
  185.             fputs(".br\n", b);
  186.             putms_verb(line + 1, b);
  187.             fputs(".br\n", b);
  188.             break;
  189.         }
  190.         case '\'':{
  191.             fputs("\\&", b);
  192.             putms(line + 1, b);
  193.             break;
  194.         }
  195.         case '.':{        /* hide leading . from ms */
  196.             fputs("\\&", b);
  197.             putms(line + 1, b);
  198.             break;
  199.         }
  200.         default:{
  201.             if (line[0] == '\n')
  202.             putms(line, b);        /* handle totally blank line */
  203.             else
  204.             putms(line + 1, b);
  205.             break;
  206.         }
  207.         break;
  208.         }
  209.         break;
  210.     }
  211.     default:{
  212.         if (isdigit((int)line[0])) {    /* start of section */
  213.         if (!intable)    /* ignore while in table */
  214.             section(line, b);
  215.         } else
  216.         fprintf(stderr, "unknown control code '%c' in column 1\n",
  217.             line[0]);
  218.         break;
  219.     }
  220.     }
  221. }
  222.  
  223.  
  224. /* process a line with a digit control char */
  225. /* starts a new [sub]section */
  226.  
  227. void section(line, b)
  228. char *line;
  229. FILE *b;
  230. {
  231.     static char string[MAX_LINE_LEN+1];
  232.     int sh_i;
  233.     static int old = 1;
  234.  
  235.  
  236.     (void) sscanf(line, "%d %[^\n]s", &sh_i, string);
  237.  
  238.     (void) fprintf(b, ".sp %d\n", (sh_i == 1) ? LINE_SKIP : LINE_SKIP - 1);
  239.  
  240.     if (sh_i > old) {
  241.     do
  242.         if (old != 1)    /* this line added by rjl */
  243.         (void) fputs(".RS\n.IP\n", b);
  244.     while (++old < sh_i);
  245.     } else if (sh_i < old) {
  246.     do
  247.         if (sh_i != 1)    /* this line added by rjl */
  248.         (void) fputs(".RE\n.br\n", b);
  249.     while (--old > sh_i);
  250.     }
  251.     /* added by dfk to capitalize section headers */
  252.     if (islower((int)string[0]))
  253.     string[0] = toupper(string[0]);
  254.  
  255.     /* next 3 lines added by rjl */
  256.     if (sh_i != 1)
  257.     (void) fprintf(b, ".NH %d\n%s\n.sp 1\n.LP\n", sh_i - 1, string);
  258.     else
  259.     (void) fprintf(b, ".NH %d\n%s\n.sp 1\n.LP\n", sh_i, string);
  260.     old = sh_i;
  261.  
  262.     (void) fputs(".XS\n", b);
  263.     (void) fputs(string, b);
  264.     (void) fputs("\n.XE\n", b);
  265. }
  266.  
  267. void putms(s, file)
  268. char *s;
  269. FILE *file;
  270. {
  271.     static boolean inquote = FALSE;
  272.  
  273.     while (*s != NUL) {
  274.     switch (*s) {
  275.     case '`':{        /* backquote -> boldface */
  276.         if (inquote) {
  277.             fputs("\\fR", file);
  278.             inquote = FALSE;
  279.         } else {
  280.             fputs("\\fB", file);
  281.             inquote = TRUE;
  282.         }
  283.         break;
  284.         }
  285.     case '\\':{        /* backslash */
  286.         fputs("\\\\", file);
  287.         break;
  288.         }
  289.     case '\'':{        /* single quote */
  290.         fputs("\\&'", file);
  291.         break;
  292.         }
  293.     default:{
  294.         fputc(*s, file);
  295.         break;
  296.         }
  297.     }
  298.     s++;
  299.     }
  300. }
  301.  
  302. /*
  303.  * convert a verbatim line to troff input style, i.e. convert "\" to "\\"
  304.  * (added by Alexander Lehmann 01/30/93)
  305.  */
  306.  
  307. void putms_verb(s, file)
  308. char *s;
  309. FILE *file;
  310. {
  311.     while (*s != '\0') {
  312.     if (*s == '\\') {
  313.         fputc('\\', file);
  314.     }
  315.     fputc(*s, file);
  316.     s++;
  317.     }
  318. }
  319.  
  320. void finish(b)            /* spit out table of contents */
  321. FILE *b;
  322. {
  323.     (void) fputs(".pn 1\n", b);
  324.     (void) fputs(".ds RH %\n", b);
  325.     (void) fputs(".af % i\n", b);
  326.     (void) fputs(".bp\n.PX\n", b);
  327. }
  328.